-
Notifications
You must be signed in to change notification settings - Fork 0
feat: #15 add album reviews #58
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code review
Three things to fix before merging:
1. The app will crash on our server
crypto.randomUUID() only works on HTTPS or localhost. Our VM runs on plain HTTP (http://it2810-19.idi.ntnu.no), so getUserId() throws and the GraphQL requests fail.
Fix: If crypto.randomUUID isn't available, make the UUID another way, for example with crypto.getRandomValues.
T19-Project-2/client/src/api/userIdentity.ts
Lines 14 to 21 in 677b33d
| export function getUserId(): string { | |
| const existing = window.localStorage.getItem(USER_ID_STORAGE_KEY); | |
| if (isUuid(existing)) return existing.toLowerCase(); | |
| const userId = crypto.randomUUID(); | |
| window.localStorage.setItem(USER_ID_STORAGE_KEY, userId); | |
| return userId; | |
| } |
2. Album links don't work on the server
The app lives under /project2/, but the links go to /album/1 and /. They leave the app, and the /^\/album\/(\d+)$/ check never matches /project2/album/1.
Fix: Start links with import.meta.env.BASE_URL, and remove that prefix from the path before running the regex.
T19-Project-2/client/src/App.tsx
Lines 9 to 11 in 677b33d
| export default function App() { | |
| const detailMatch = /^\/album\/(\d+)$/.exec(window.location.pathname); | |
| const detailId = detailMatch?.[1]; |
T19-Project-2/client/src/components/AlbumCard.tsx
Lines 9 to 11 in 677b33d
| <h3 id={`album-${album.id}`}> | |
| <a href={`/album/${album.id}`}>{album.title}</a> | |
| </h3> |
3. The page flashes "Loading album…" after every save or delete
refetch() sets loading back to true, so the whole page is replaced by the loading text and focus is lost. That's bad for accessibility.
Fix: Change if (loading) to if (loading && !data).
T19-Project-2/client/src/pages/AlbumDetailPage.tsx
Lines 20 to 31 in 677b33d
| export default function AlbumDetailPage({ id }: { id: string }) { | |
| const { data, loading, error, refetch } = useQuery(ALBUM_DETAIL, { | |
| variables: { id, reviewFirst: REVIEW_PAGE_SIZE, reviewAfter: null }, | |
| }); | |
| if (loading) { | |
| return ( | |
| <main id="main" className="album-detail" tabIndex={-1} aria-busy="true"> | |
| <p role="status">Loading album…</p> | |
| </main> | |
| ); | |
| } |
Small extra: when a review is edited, updatedAt isn't updated. Add updatedAt: new Date() to the set in onConflictDoUpdate.
T19-Project-2/server/src/reviews.ts
Lines 67 to 72 in 677b33d
| }) | |
| .onConflictDoUpdate({ | |
| target: [review.userId, review.albumId], | |
| set: { rating: input.rating, comment: input.comment.trim() }, | |
| }) | |
| .returning(); |
677b33d to
736a446
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Review
Reviewed as the delta against feat/11-album-details, since this is stacked on #57.
The identity contract is the part I checked hardest, and it holds up. X-User-Id is attached with setContext around the HttpLink, so it goes on every request and not only the mutations. parseUserId runs per request in the Yoga context and nothing is hoisted to module scope, so one browser's identity cannot leak into another request. myReviewByAlbum degrades to all-null when there is no user, which is the anonymous behaviour we agreed on.
removeReview scopes the delete with and(eq(review.id, id), eq(review.userId, ownerId)), so a user cannot delete someone else's review. onConflictDoUpdate targeting [review.userId, review.albumId] enforces one review per user per album without a round trip. ensureUser runs after findAlbum, so a bad album id does not leave an orphan user behind. Good.
The star input is a real radio group: fieldset, legend, shared name, and each input wrapped in its own label. That satisfies the radio-group requirement in #17 as written.
Findings
1. updatedAt in the upsert is dead code (low)
.set({ rating: ..., comment: ..., updatedAt: new Date() })review_set_updated_at is a BEFORE UPDATE trigger doing NEW.updated_at := now(), so it overwrites whatever is set here. Harmless, but it reads as if the resolver owns updated_at when the trigger does. Same class of confusion as average_rating, which is why I am calling it out: #12 is adding a trigger-owned average_rating, and the resolver should not write that either.
Worth deleting the line and leaving a comment that the trigger owns it.
2. Delete is one click and irreversible (medium-low)
remove() fires immediately with no confirmation. A review with a written comment is gone on a stray click. A confirm step, or an undo window, would be kinder.
3. catch {} throws away the server's message (medium-low)
Both handlers swallow the error and show a generic string. The server has specific, actionable messages - A valid X-User-Id header is required for this action, Rating must be an integer between 1 and 5. The user currently gets "Your review could not be saved" for all of them. Surfacing error.message when it is a GraphQLError would be cheap.
4. Comment length is unbounded (medium-low)
Neither the textarea nor saveReview limits the comment. This is a public write path, so a very large payload is a free way to bloat the table and the detail page. A maxLength on the input plus a server-side check would close it.
5. Two plain Error throws (low)
if (!saved) throw new Error('Could not save review.') and the equivalent in ensureUser. Everything else in the module uses GraphQLError, and a plain error surfaces as an internal server error rather than a clean client-facing one.
6. localStorage access is unguarded (low)
getUserId() calls window.localStorage.getItem and setItem directly, and it sits on the request path for every call. In a restricted browsing context that can throw, which would fail every request rather than one. A try/catch falling back to an in-memory id would make it degrade instead.
On merging
Nothing here blocks the merge in my view. 1, 3 and 4 are small and worth folding in while the file is fresh. 2 and 6 are judgement calls. 5 is consistency.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for addressing the previous feedback! 😊 The functionality looks good overall.
I found two remaining issues that I think should be fixed before merging, and I've left inline comments on both.
One small UI observation from testing locally: the album detail page and review form look a bit different from our UI sketch, particularly the layout, rating inputs and "Back to discovery" link. Some styling adjustments might be worth considering, either in this PR or as part of the final UI polish. I'll leave that up to you, as it's not a blocker for this PR!
a7d1ce3 to
eef4e8b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good now! Thanks for addressing the feedback 😊
eef4e8b to
da3cf58
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Everything looks good after updating the base branch. Ready to merge! 😊
Closes #15
Summary
X-User-Idwith GraphQL requests and validates it in the server context.upsertReviewanddeleteReview, enforcing one review per user and album.role="alert"errors, update and delete.Verification
pnpm format:checkpnpm lintpnpm -r buildStacked PR
This PR targets #57 (
feat/11-album-details) because it adds interactive reviews to the detail page delivered there. Review and merge #57 first; then this PR can be retargeted tomain.